diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py
index 2e97d9064b..f257e947ba 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -248,12 +248,12 @@ class Command(BaseCommand):
                 else:
                     self.log("No changes detected")
         else:
+            if check_changes:
+                sys.exit(1)
             if self.update:
                 self.write_to_last_migration_files(changes)
             else:
                 self.write_migration_files(changes)
-            if check_changes:
-                sys.exit(1)
 
     def write_to_last_migration_files(self, changes):
         loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
